package com.zqh.middleware.whitelist;

import com.alibaba.fastjson.JSON;
import com.zqh.middleware.whitelist.annotation.DoWhiteList;
import org.apache.commons.beanutils.BeanUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import javax.annotation.Resource;

/**
 * @author 曾庆红
 * @create 2024-03-19 15:20
 */
@Aspect
@Component
public class DoJoinPoint {
    private Logger logger = LoggerFactory.getLogger(DoJoinPoint.class);

    @Resource
    private String  whiteListConfig;

    @Pointcut("@annotation(com.zqh.middleware.whitelist.annotation.DoWhiteList)")
    public  void aopPoint(){
    }
    @Around("aopPoint()")
    public Object doRouter(ProceedingJoinPoint joinPoint) throws Throwable{
        //获取内容
        Method method = getMethod(joinPoint);
        DoWhiteList doWhiteList = method.getAnnotation(DoWhiteList.class);

        //获取字段值
        String keyValue =getFiledValue(doWhiteList.key(),joinPoint.getArgs());
        logger.info("middleware whitelist handler method：{} value：{}", method.getName(), keyValue);
        if (null == keyValue || "".equals(keyValue)) return joinPoint.proceed();

        String[] split = whiteListConfig.split(",");

        // 白名单过滤
        for(String str : split){
            if (keyValue.equals(str)){
                return joinPoint.proceed();
            }
        }

        //拦截
        return returnObject(doWhiteList,method);
    }

    private Object returnObject(DoWhiteList doWhiteList, Method method) throws InstantiationException, IllegalAccessException {
        Class<?> returnType = method.getReturnType();
        String returnJson = doWhiteList.returnJson();
        if ("".equals(returnJson)){
            return  returnType.newInstance();
        }
        return JSON.parseObject(returnJson,returnType);
    }

    private String getFiledValue(String key, Object[] args) {
        String filedValue = null;
        for (Object arg: args){
            try{
                if (null == filedValue || "".equals(filedValue)){
                    filedValue = BeanUtils.getProperty(arg,key);
                }else{
                    break;
                }
            }catch (Exception e){
                if (args.length == 1){
                    return args[0].toString();
                }
            }
        }
        return filedValue;
    }

    private Method getMethod(ProceedingJoinPoint joinPoint) throws NoSuchMethodException {
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature)signature;
        return joinPoint.getTarget().getClass().getMethod(methodSignature.getName(),methodSignature.getParameterTypes());
    }
}
